package org.springcloud.plugin.strategy.gateway.context;

import lombok.extern.slf4j.Slf4j;
import org.springcloud.plugin.framework.strategy.context.AbstractStrategyContextHolder;
import org.springframework.web.server.ServerWebExchange;

/**
 * author: yanggld
 * date: 2019/12/21 21:59
 * version: 1.0
 * description: GatewayStrategyContextHolder
 */
@Slf4j
public class GatewayStrategyContextHolder extends AbstractStrategyContextHolder {

    public ServerWebExchange getExchange() {
        return GatewayStrategyContext.getCurrentContext().getExchange();
    }

    @Override
    public String getHeader(String name) {
        ServerWebExchange exchange = getExchange();
        if (exchange == null) {
            log.warn("The ServerWebExchange object is lost for thread switched, or it is got before context filter probably");
            return null;
        }

        return exchange.getRequest().getHeaders().getFirst(name);
    }
}
